package com.oasistudio.ads.expert.banner;

import android.app.Activity;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.ads.Ad;
import com.google.ads.AdListener;
import com.google.ads.AdRequest.ErrorCode;
import com.oasistudio.ads.expert.R;

public class AdViewGoogle {
	GoogleAdRequestListener adRequestListener;
	Activity activity;
	public static final String UNION = "admob.com";
	public AdViewGoogle(Activity activity){
		this.activity = activity;
		this.adRequestListener = new GoogleAdRequestListener(activity, UNION);
		
		addGoogleAd();
	}
	
	public com.google.ads.AdView getAdView(){
		return this.googleAdView;
	}
	
	/************************** google admob.com start *************************/
	private com.google.ads.AdView googleAdView;
	private void addGoogleAd() {
		LinearLayout adContainer = (LinearLayout) activity.findViewById(R.id.ads_container);
		adContainer.addView(new TitleBuilder().createTitle(activity, UNION));

		LinearLayout googleContainer = new LinearLayout(activity); 
		adContainer.addView(googleContainer);
		
		String MY_AD_UNIT_ID = "a14ee1fc7e04a00";
		googleAdView = new com.google.ads.AdView(activity, com.google.ads.AdSize.BANNER, MY_AD_UNIT_ID);
		googleAdView.setAdListener(this.adRequestListener);
		googleAdView.loadAd(new com.google.ads.AdRequest());
//		googleAdView.setOnClickListener(new View.OnClickListener() {
//			
//			@Override
//			public void onClick(View v) {
//				// TODO Auto-generated method stub
//				System.out.println("&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&googleAdView.onClick()");
//			}
//		});
		googleContainer.addView(googleAdView, new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
	}

	/************************** google admob.com end *************************/
	class GoogleAdRequestListener extends AbstractAdsExpertAdListener implements AdListener{
		
		public GoogleAdRequestListener(Activity activity, String union){
			super(activity, union);
		}

		@Override
		public void onDismissScreen(Ad arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onFailedToReceiveAd(Ad arg0, ErrorCode arg1) {
			// TODO Auto-generated method stub
			this.fail();
		}

		@Override
		public void onLeaveApplication(Ad arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onPresentScreen(Ad arg0) {
			// TODO Auto-generated method stub
			
		}

		@Override
		public void onReceiveAd(Ad arg0) {
			// TODO Auto-generated method stub
			this.succeed();
			AdHistory.saveAd(UNION, googleAdView);
		}

	}
}
